<?php
/**
 * Created by PhpStorm.
 * User: ZhangYi
 * Date: 2011-1-14
 * Time: 15:29:22
 * To change this template use File | Settings | File Templates.
 */

class ScwsTestTask extends sfBaseTask {

    protected function configure() {
        parent::configure();
        $this->addOptions(array(new sfCommandOption('input', null,
            sfCommandOption::PARAMETER_REQUIRED, 'input string', null)));
        $this->namespace = 'ieasy';
        $this->name = 'scwsTest';
    }

    private $tokenList;
    private $tokenKeys;

    protected function execute($arguments = array(), $options = array()) {
        $databaseManager = new sfDatabaseManager($this->configuration);

        $input = $options['input'];

        $so = scws_new();
        $so->set_charset('utf-8');
        $so->set_dict('/usr/local/scws/etc/dict.utf8.xdb');
        $so->set_multi(7);
        $so->send_text($input);


        $this->tokenList = array();
        $this->tokenKeys = array();

        while ($tmp = $so->get_result())
        {
            foreach ($tmp as $word)
            {
                $str = $word['word'];

                if (!in_array($str, $this->tokenKeys)) {
                    $strlen = mb_strlen($str, 'utf-8');
                    $off = 0;
                    while ($off < mb_strlen($input, 'utf-8')
                            && ($pos = mb_strpos($input, $str, $off, 'utf-8'))!==FALSE) {
                        $endpos = $pos + $strlen;
                        $off += $pos + $strlen;
                        echo $str . " " . $pos . " " . $endpos, "\n";

                    }

                    $this->tokenKeys[] = $str;

                }
            }

        }
        echo "\n";
        print_r($this->tokenKeys);
        $so->close();


        //        $this->strposTest();
    }


    private function strposTest() {
        $str = "元旦期间我国大部将有雨雪 气温持续偏低";
        $findme = "元旦";
        echo mb_strlen($str, 'utf-8') . "\n";
        echo mb_strpos($str, $findme, null, 'utf-8') . "\n";
    }
}
